# Maintainer: Maciej Suminski <maciej.suminski@cern.ch>
#
# Based on Arch ngspice PKGBUILD:
# https://git.archlinux.org/svntogit/community.git/tree/trunk/PKGBUILD?h=packages/ngspice
#   Contributor: Kyle Keen <keenerd@gmail.com>
#   Contributor: Abhishek Dasgupta <abhidg@gmail.com>
#   Contributor: Jason Taylor <jftaylor21@gmail.com>
#   Contributor: Luis Henrique <lmello.009@gmail.com>

_realname=ngspice
pkgbase=mingw-w64-${_realname}
pkgname="${MINGW_PACKAGE_PREFIX}-${_realname}"
pkgver=26
pkgrel=2
pkgdesc='Mixed-level/Mixed-signal circuit simulator based on Spice3f5, Cider1b1, and Xspice.'
url='http://ngspice.sourceforge.net'
license=('BSD')
arch=('any')
makedepends=("${MINGW_PACKAGE_PREFIX}-gcc")
depends=("${MINGW_PACKAGE_PREFIX}-gcc-libs")
source=("http://downloads.sourceforge.net/project/${_realname}/ng-spice-rework/${pkgver}/${_realname}-${pkgver}.tar.gz"
        "http://downloads.sourceforge.net/project/${_realname}/ng-spice-rework/${pkgver}/${_realname}-doc-${pkgver}.tar.gz")
sha256sums=('51e230c8b720802d93747bc580c0a29d1fb530f3dd06f213b6a700ca9a4d0108'
            'adc722cf627ee4506ea612c3fcb79f9c73c497e328547ab2d1ea97da588459cb')

build() {
  cd "${srcdir}/${_realname}-${pkgver}"
  [[ -d "${srcdir}/build-shared-${CARCH}" ]] && rm -rf "${srcdir}/build-shared-${CARCH}"
  mkdir -p "${srcdir}/build-shared-${CARCH}" && cd "${srcdir}/build-shared-${CARCH}"

  # FS#45230, create so lib
  # shared lib sets flags and modifies headers, needs dedicated pass
  # adding --with-readline disables libngspice-0.dll
  ../${_realname}-${pkgver}/configure \
              --prefix=${MINGW_PREFIX} \
              --build=${MINGW_CHOST} \
              --host=${MINGW_CHOST} \
              --enable-openmp \
              --enable-xspice \
              --enable-cider \
              --with-ngshared
  make

  cd "${srcdir}/${_realname}-${pkgver}"
  [[ -d "${srcdir}/build-static-${CARCH}" ]] && rm -rf "${srcdir}/build-static-${CARCH}"
  mkdir -p "${srcdir}/build-static-${CARCH}" && cd "${srcdir}/build-static-${CARCH}"

  ../${_realname}-${pkgver}/configure \
              --prefix=${MINGW_PREFIX} \
              --build=${MINGW_CHOST} \
              --host=${MINGW_CHOST} \
              --enable-xspice \
              --enable-cider \
              --enable-openmp
  make
}

package() {
  cd "${srcdir}/build-static-${CARCH}"
  make install DESTDIR="${pkgdir}"
  install -D -m644 "${srcdir}/${_realname}-${pkgver}/COPYING" "${pkgdir}${MINGW_PREFIX}/share/licenses/${_realname}/LICENSE"
  install -D -m644  "${srcdir}/${_realname}-doc-${pkgver}/manual.pdf" "${pkgdir}${MINGW_PREFIX}/share/doc/${_realname}/manual.pdf"

  # ngspice installs headers to /${PREFIX}/share/ngspice/include, which is not the usual location for *.h files
  mv ${pkgdir}${MINGW_PREFIX}/share/ngspice/include ${pkgdir}${MINGW_PREFIX}

  # library files have to be copied manually
  cd "${srcdir}/build-shared-${CARCH}"
  install -D -m755 src/.libs/libngspice-0.dll "${pkgdir}${MINGW_PREFIX}/bin"
  install -D -m755 src/.libs/libngspice.dll.a "${pkgdir}${MINGW_PREFIX}/lib"
}
